﻿using CTP.CRM.Core.SharedModel.Pub;
using System.Net.Http.Headers;

namespace CTP.CRM.Core.SharedModel
{

    public static class HttpClientAuthenticationHeaderExtentions
    {
        //
        // 摘要:
        //     用于访问下游服务，无法获取前端Token的情况下手动设置Token
        //
        // 参数:
        //   httpClient:
        //
        //   mockToken:
        public static HttpClient SetMockTokenHeader(this HttpClient httpClient, string mockToken)
        {
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", mockToken);
            return httpClient;
        }

        //
        // 摘要:
        //     用于设置不同微服务调用中Token同步
        //
        // 参数:
        //   httpClient:
        public static HttpClient SetTokenHeader(this HttpClient httpClient)
        {
            IHeaderDictionary headerDictionary = VerfiyHeader();
            if (headerDictionary == null)
            {
                string str = TokenGeneratorHelper.GeneratorSystemToken();
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", str.ReplaceFirst("Bearer", "").TrimStart());
                return httpClient;
            }

            string value = headerDictionary["SystemRequest"];
            if (!string.IsNullOrEmpty(value))
            {
                httpClient.DefaultRequestHeaders.Add("SystemRequest", value);
                return httpClient;
            }

            string text = headerDictionary["Authorization"];
            if (string.IsNullOrEmpty(text))
            {
                return httpClient;
            }

            string parameter = text.ReplaceFirst("Bearer", "").TrimStart();
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", parameter);
            return httpClient;
        }

        //
        // 摘要:
        //     用于设置不同微服务调用中子系统id同步
        //
        // 参数:
        //   httpClient:
        public static HttpClient SetSubSysHeader(this HttpClient httpClient)
        {
            IHeaderDictionary headerDictionary = VerfiyHeader();
            if (headerDictionary == null)
            {
                return httpClient;
            }

            string value = headerDictionary["SubSystemId"];
            httpClient.DefaultRequestHeaders.Add("SubSystemId", value);
            return httpClient;
        }

        //
        // 摘要:
        //     用于设置不同微服务调用中业务主体id同步
        //
        // 参数:
        //   httpClient:
        public static HttpClient SetBusinessHeader(this HttpClient httpClient)
        {
            IHeaderDictionary headerDictionary = VerfiyHeader();
            if (headerDictionary == null)
            {
                return httpClient;
            }

            string value = headerDictionary["BusinessId"];
            httpClient.DefaultRequestHeaders.Add("BusinessId", value);
            return httpClient;
        }

        private static IHeaderDictionary VerfiyHeader()
        {
            if (CTPABPCore.HttpContext != null && CTPABPCore.HttpContext?.Request != null)
            {
                return CTPABPCore.HttpContext?.Request?.Headers ?? null;
            }

            return null;
        }
    }
}
